/*******************************************************************************
Last update: March 26, 2023 
********************************************************************************/

cls
clear all
local dir "~\Dropbox\Working Papers\Distinction Effect"
cd "`dir'"

/********************************************************************************/

//Industries 
//use "Data\Intermediates\IndystryCodes_CIIU3.dta", clear

use "Data\Intermediates\IndystryCodes_CIIU3_31.dta", clear
gen industry_code = industry_code3 
replace industry_code = industry_code31 if industry_code == ""

keep ciiu industry_code

//Fields of Study (Majors)
local fields 11 12 13 24 25 26 27 28 270 440 442 444 445 441 447 448 450 446 558 561 559 313 564 562 566 568 569 553 555 556 557 611 612 69 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 934 935 936 937 939

foreach i of local fields {
	gen field_`i' = 1 
}

//Field by Industry Data
reshape long field_, i(ciiu) j(field_code) string
order ciiu industry_code field_code
drop field_

//Field-Industry Match 

cap program drop fieldmatch
program fieldmatch
syntax , field(varname) fieldcode(string) industry(varname) industrycode(string) replace(varname)
foreach i in `industrycode' {
	if length("`i'") == 4 {
	replace `replace' = 1 if `field' == "`fieldcode'" & `industry' == "`i'"
	}
	if length("`i'") == 3 {
	replace `replace' = 1 if `field' == "`fieldcode'" & substr(`industry', 1, 3) == "`i'"
	}
	if length("`i'") == 2 {
	replace `replace' = 1 if `field' == "`fieldcode'" & substr(`industry', 1, 2) == "`i'"
	}
}
end

//Variables to Store Match
//Researcher 1
gen fieldciiu_match1 = 0
//Researcher 2
gen fieldciiu_match2 = 0

//Agronomy
local fieldcode "11"
local industrycode 01 05 151 152 153 154 156 157 158 159 5111 5112 5121 5122 5123 5124 5125 5126 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 01 151 152 153 154 156 157 158 159 16 5111 5112 512 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Zootechnics
local fieldcode "12"
local industrycode 0121 0122 0124 0125 013 014 015 05 151 153 5124 9233
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 012 014 015 05 151 153 1543 5111 5124 731 803 9233
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Veterinary
local fieldcode "13"
local industrycode 0121 0122 0124 0125 8520
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 012 8520
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Advertisement
local fieldcode "26"
local industrycode 22 642 7320 7413 7414 7430 921 922
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7413 7414 7430 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Clinical Lab (Bacteriology)
local fieldcode "440"
local industrycode 7310 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 2423 5135 8511 8512 8514 8519 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Therapy
local fieldcode "442"
local industrycode 8511 8512 8514 8515 8519 7491
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 8511 8512 8514 8515 8519 7491
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Surgical Instrument
local fieldcode "444"
local industrycode 3311 5136 8511 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 3311 5136 8511 8512 8514 8519 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Medicine
local fieldcode "445"
local industrycode 7310 8511 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 731 8511 8512 8514 8515 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Nursery
local fieldcode "441"
local industrycode 8511 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 8511 8512 8514 8519 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Dentistry
local fieldcode "447"
local industrycode 8513 8514 851
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 8513 8514 851
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Optometry
local fieldcode "448"
local industrycode 3311 5136 8511 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 3311 5136 5246 8511 8512 8514 8519  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Nutrition and Diet
local fieldcode "446"
local industrycode 8511 8512 8514 8519 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 15 512 8511 8512 8514 8519  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Law
local fieldcode "559"
local industrycode 7411 7491 7492 7511 7513 7514 7515 7519 7521 7523 803
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7411 7412 7511 7512 7523 8030 911
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Education
local fieldcode "313"
local industrycode 22 7320 80 923
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 2211 80 7320 923  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Modern Languages
local fieldcode "564"
local industrycode 22 7320 80 9232
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7512 7521 8030 9232 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//History and Geography
local fieldcode "562"
local industrycode 7320 7512 7519 80 9120 9112 9192 9199 922 923
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7320 7512 802 803 9231 9232  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Psychology
local fieldcode "566"
local industrycode 7491 8511 8512 8514 8515 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 732 7491 802 803 809 8511 8512 8515 8519 7413 7512 911 912
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Sociology and Social Work
local fieldcode "569"
local industrycode 7320 7512 7515 802 803 9112 9120 9192 9199 9232
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7320 7512 802 803 853 91 9309 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Political Science and International Relations
local fieldcode "556"
local industrycode 7320 7413 7414 7512 7513 7514 7515 7519 7521 7522 7523 802 803 9112 9120 9192 990
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 7521 8030 9192
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Journalism and Communications
local fieldcode "557"
local industrycode 22 64 921 922 923 7320 7430
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 22 64 7240 7320 7430 8030 9213 9219 922
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Economics
local fieldcode "611"
local industrycode 65 66 67 723 7320 7412 7413 7414 7422 7512 802 803 9192
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 51 65 66 67 7320 7512 7521 802 803 911 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Accounting
local fieldcode "612"
local industrycode 6513 6514 6515 659 66 6711 6712 6713 6714 6717 6719 672 7412 7414 7512 7513 7514
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 65 66 67 70 72 7412 7414 7421 7491 7512 8023 911 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Business
local fieldcode "69"
local industrycode 501 503 504 51 52 65 66 67 7412 7413 7414 7430 7491 7512 7514 7515 7519 9111
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 16 51 5511 65 66 67 7413 7414 7491 7512 7515 8511 911  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Biomedical Engineering
local fieldcode "819"
local industrycode 7310 3311 5136 8512 8514 8519
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 3311 5136 851 731 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Environmental Eng.
local fieldcode "820"
local industrycode 10 11 12 13 14 451 453 7310 7421 9233
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 10 11 12 13 14 37 41 451 453 7421 7422 90 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Business Eng
local fieldcode "821"
local industrycode 501 503 504 51 65 66 67 7412 7413 7414 7430 7491 7512 7514 7515 7519 9111 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 51 65 66 67 7413 7414 7491 7512 7515 911 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Forest and Agricultural Eng.
local fieldcode "822"
local industrycode 01 02 05 2412 2421 2921 5111 5112 5121 5122 5123 5124 5125 5126 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 01 02 05  5121 5122 5123 5124 5125 5126 5161 731 7421 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Agroindustrial Eng.
local fieldcode "823"
local industrycode 01 02 05 151 152 153 154 156 157 2412 2421 2921 5111 5112 5121 5122 5123 5124 5125 5126 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 01 02 05 15 16 2412 2421 5111 5112 5121 5122 5123 5124 5125 5126 5161 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Livestock and Agronomic Eng.
local fieldcode "824"
local industrycode 01 05 151 152 153 154 156 157 158 159 5111 5112 5121 5122 5123 5124 5125 5126 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 01 151 152 153 154 156 157 158 159 16 5111 5112 512 5161
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Civil Eng.
local fieldcode "825"
local industrycode 451 452 453 456 742
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 351 352 353 40 41 451 452 453 4541 456 742
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Metallurgical and Mining Eng.
local fieldcode "826"
local industrycode 10 11 12 13 14 27 28 4512 4530 5151 5152 5161 5169
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 10 11 12 13 14 27 28 451 5151 5152 5161 7421 7422 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//System Eng.
local fieldcode "827"
local industrycode 5164 72
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 51 64 65 66 67 72 7421 8511 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Electric Eng.
local fieldcode "828"
local industrycode 31 401 4530 4541 4542 7421 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 31 401 4542 7421 742  
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Electronic Eng.
local fieldcode "829"
local industrycode 31 32 454 5165 642 725 7421
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 31 32 33 5165 642 72 7421 7492 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Industrial Eng.
local fieldcode "830"
local industrycode 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 50 51 60 61 62 64 65 66 67 70 71 73 751
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 11 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 40 5011 51 60 61 62 631 632 639 642 65 711 712 731 7421 7491 7414 67
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Mechanical Eng.
local fieldcode "831"
local industrycode 10 11 12 13 14 2813 2892 29 31 34 35 4530 4541 4543 4560 501 502 503 504 5161 5162 5169 5170 7310 7421
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 10 11 12 13 14 25 26 27 281 2891 2892 29 311 312 331 34 35 453 456 5161 5162 5170 7421 7422
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Chemical Eng.
local fieldcode "832"
local industrycode 11 12 13 15 19 21 23 24 25 26 5153 5155 7421 7310
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 11 15 23 24 2610 5153 5155 731 7421 7422 90
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Biology
local fieldcode "934"
local industrycode 011 012 013 020 050 2412 2421 2423 7310 7422 7513 8012 802 803
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 011 012 013 015 02 05 2412 2421 731 8012 802 803
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Physics
local fieldcode "935"
local industrycode 2321 2322 2330 2411 80 7310
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 232 233 7230 731 802 803 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Geology
local fieldcode "936"
local industrycode 10 11 13 14 451 452 453 7122 7310 7421
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 10 11 12 13 14 732 7421 
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Math and Statistics
local fieldcode "937"
local industrycode 65 66 6711 6712 6713 6714 6717 6719 672 7210 7230 7240 7290 7310 7413 7422 80 7310
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 65 66 67 722 723 729 73 7413 7512 7515 80 8511
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)
//Chemistry
local fieldcode "939"
local industrycode 1421 24 5151 5152 5153 5155 7310 80
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 1421 15 24 731 7422 80
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)

/********************************************************************************
Fields without Specific Exams by 2009
********************************************************************************/

//Sports and Physical Education
local field "558"
local industry 80 8515 8519 9219 922 9241 9249
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match1)
local industrycode 80 8515 8519 9241 9249
fieldmatch, field(field_code) industry(industry_code) fieldcode(`fieldcode') industrycode(`industrycode') replace(fieldciiu_match2)

//Public Health
local field "450"
//Military or Police Education 
local field "561"
//Philosophy 
local field "568"
//Antropology 
local field "553"
//Library Science
local field "555"
//Architecture
local field "818"
//Plastic and Visual Arts
local field "24"
//Representative Arts
local field "25"
//Design
local field "27"
//Music
local field "28"
//Other Related Engineering Programs
local field "833"
//Other Related Arts Programs
local field "270"

destring field_code, replace
save "Data\Finals\Field-Indystry Match.dta", replace
